package com.csun.dsms.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.JyDataMapper;
import com.csun.dsms.mapper.JyProblemManagementRecordMapper;
import com.csun.dsms.model.education.JyProblemManagement;
import com.csun.dsms.model.education.JyProblemManagementRecord;
import com.csun.dsms.service.JyProblemManagementRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Slf4j
@Service
public class JyProblemManagementRecordServiceImpl implements JyProblemManagementRecordService {

    @Resource
    private  JyProblemManagementRecordMapper jyProblemManagementRecordMapper;
    @Resource
    private JyDataMapper jyDataMapper;

    @Override
    public Wrapper<Object> addManagementRecord(JyProblemManagementRecord jyProblemManagementRecord) {
        try {
            Long managementId = jyProblemManagementRecord.getManagementId();
            if (managementId == null) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "管理ID不能为空");
            }
            JyProblemManagement jyProblemManagement = jyDataMapper.selectByPrimaryKey(managementId);
            if (jyProblemManagement == null) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "管理数据不存在");
            }
            jyProblemManagementRecordMapper.insert(jyProblemManagementRecord);
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, null);
        } catch (Exception e) {
            log.error("新增纪录数据报错", e);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }

    @Override
    public Wrapper<Object> queryManagementRecordStatistics(JSONObject reqJson) {
        try {
            List<JyProblemManagement> problemManagementList = jyDataMapper.queryProblemManagementPage(reqJson);
           if (problemManagementList.isEmpty()) {
               return WrapMapper.wrap(Wrapper.ERROR_CODE, "未查询到数据");
           }
            List<Object> resList = CollectionUtil.newArrayList();
            for (JyProblemManagement jyProblemManagement : problemManagementList) {
                Long id = jyProblemManagement.getId();
                String prominentProblem = jyProblemManagement.getProminentProblem();
                JyProblemManagementRecord jyProblemManagementRecord = new JyProblemManagementRecord();
                jyProblemManagementRecord.setManagementId(id);
                List<JyProblemManagementRecord> jyProblemManagementRecordList = jyProblemManagementRecordMapper.select(jyProblemManagementRecord);
                if(CollectionUtil.isNotEmpty(jyProblemManagementRecordList)){
                    jyProblemManagementRecordList.sort(Comparator.comparing(JyProblemManagementRecord::getCreatedTime).reversed());
                    JyProblemManagementRecord jyProblemManagementRecordNew = jyProblemManagementRecordList.get(0);
                    jyProblemManagementRecordNew.setProminentProblem(prominentProblem);
                    resList.add(jyProblemManagementRecordNew);
                }
            }
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, resList);
        } catch (Exception e) {
            log.error("查询数据报错", e);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }
}
